package com.gl.goods.ui.main.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.gl.goods.R;
import com.gl.goods.base.BaseMvpFragment;
import com.gl.goods.ui.main.adapter.ImageAdapter;
import com.gl.goods.ui.main.bean.HomeAdvertisementListBean;
import com.gl.goods.ui.main.mvp.home.HomeContract;
import com.gl.goods.ui.main.mvp.home.HomePresenter;
import com.gl.library.utils.StringUtils;
import com.gl.library.widget.textbanner.TextBannerView;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.transformer.RotateDownPageTransformer;
import com.youth.banner.util.BannerUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Time; 2020/11/13
 * Author: hmc
 * Description:
 */
public class HomeFragment extends BaseMvpFragment<HomePresenter> implements HomeContract.View {
    /**
     * 下拉刷新
     */
    @BindView(R.id.home_refresh)
    RefreshLayout smartRefreshLayout;
    /**轮播图*/
    @BindView(R.id.banner)
    Banner mBanner;
    public static List<String> images;
    private List<HomeAdvertisementListBean.DataDTO> bannerDateBean;
    /**公告*/
    @BindView(R.id.tv_gonggao)
    TextBannerView tv_gonggao;

    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        mPresenter = new HomePresenter();
        mPresenter.attachView(this);
        //获取轮播图数据
        mPresenter.getAdvertisementList();

        /**
         * 页面整体下拉刷新
         */
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //获取轮播图数据
                mPresenter.getAdvertisementList();
                refreshLayout.finishRefresh(1000);
            }
        });
        /**
         * 列表加载更多
         */
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
//                if (StringUtils.isNotNull(goodsListBean)) {
//                    if (goodsAdapter.getData().size() >= goodsListBean.getTotal_count()) {
//                        refreshLayout.finishLoadMoreWithNoMoreData();
//                    } else {
//                        showDialogLoading(R.string.loading);
//                        curPage++;
//                        getHomeCommentGoodsList(curPage, HXYXConstant.PAGE_SIZE_10);
//                        refreshLayout.finishLoadMore(1000);
//                    }
//                } else {
//                    refreshLayout.finishLoadMoreWithNoMoreData();
//                }
            }
        });
    }

    @Override
    public void getAdvertisementSuccess(HomeAdvertisementListBean bannerBean) {
        if (!isDestroy(mContext))
        bannerDateBean = bannerBean.getData();
        if (StringUtils.isNotEmpty(bannerDateBean) && bannerDateBean.size() > 0) {
            images = new ArrayList<>();
            for (int i = 0; i < bannerDateBean.size(); i++) {
                images.add(bannerDateBean.get(i).getImgUrl());
                if (StringUtils.isNotNull(mBanner)){
                    mBanner.setAdapter(new ImageAdapter(images,mContext));
                    mBanner.setIndicator(new CircleIndicator(mContext));
                    mBanner.setPageTransformer(new RotateDownPageTransformer());
                    mBanner.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
                    mBanner.setIndicatorMargins(new IndicatorConfig.Margins(0, 0,
                            0, (int) BannerUtils.dp2px(20)));
                }
            }
        }
    }

    @Override
    public void showLoading() {
        showDialogLoading(R.string.loading);
    }

    @Override
    public void hideLoading() {
        hideDialogLoading();
    }

    @Override
    public void showErrorMessage(String error_message) {
        ToastUtils.show(error_message);
    }
}
